package com.bd.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.bd.components.AppFilterPanel;
import com.bd.components.AppTable;
import com.bd.components.AppTableRender;
import com.bd.components.IAppPanel;
import com.bd.constants.ResourcesConstants;
import com.bd.controller.AppMainController;
import com.bd.model.Compositor;
import com.bd.model.Faixa;
import com.bd.util.SwingUtil;
import com.jgoodies.forms.layout.FormLayout;

public class ComposerView extends JPanel implements IAppPanel<Compositor> {

	/**
	 * 
	 */
	private static final long		serialVersionUID	= 3690377757468632843L;
	private AppFilterPanel			appFilterPanel		= new AppFilterPanel(Compositor.class);
	private String					columns				= "3dlu, default:grow";
	private String					lines				= "3dlu, default, 3dlu, default:grow";
	private JPanel					resultPanel			= new JPanel();
	private JScrollPane				scrollPane			= new JScrollPane();
	private AppTable<Compositor>	appTable			= new AppTable<Compositor>(this, Compositor.class);

	/**
	 * Create the panel.
	 */
	public ComposerView() {
		initComponents();
		populateTable();
	}

	private void initComponents() {
		setLayout(new FormLayout(columns, lines));

		add(appFilterPanel, "2, 2, fill, fill");
		add(resultPanel, "2, 4, fill, fill");

		appTable.addColumn("ID");
		appTable.addColumn("Nome");
		appTable.addColumn("Local Nascimento");
		appTable.addColumn("Data Nascimento");
		appTable.addColumn("Data Morte");
		appTable.addColumn("Faixa");

		resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.X_AXIS));
		resultPanel.add(scrollPane);
		resultPanel.setBorder(BorderFactory.createTitledBorder("Compositores"));
		scrollPane.setViewportView(appTable);
		getFilter().addFilterListener(new ComposerFilterLister());
		appTable.getColumnModel().getColumn(0).setCellRenderer(new AppTableRender(ResourcesConstants.ICON_COMPOSER_16));
	}

	@Override
	public void populateTable() {
		SwingUtil.clearTable(appTable);

		AppMainController ctrl = AppMainController.getInstance();

		List<Compositor> composers = ctrl.findAllComposers();
		Faixa track = null;

		for (Compositor composer : composers) {
			track = ctrl.findTrackByComposer(composer);
			appTable.addRow(new Object[] { composer.getId(), composer, composer.getLocalNascimento(), composer.getDataNascimento(),
							composer.getDataMorte(), track });
		}
	}

	@Override
	public AppFilterPanel getFilter() {
		return appFilterPanel;
	}

	class ComposerFilterLister implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			SwingUtil.clearTable(appTable);

			AppMainController ctrl = AppMainController.getInstance();

			List<Compositor> composers = ctrl.findComposerByParameter(getFilter().getFilterColumn(), getFilter().getFilterText());

			Faixa track = null;

			for (Compositor composer : composers) {
				track = ctrl.findTrackByComposer(composer);
				appTable.addRow(new Object[] { composer.getId(), composer, composer.getLocalNascimento(), composer.getDataNascimento(),
								composer.getDataMorte(), track });
			}
		}
	}

	@Override
	public Compositor getSelectedObject() {
		return appTable.getSelectedObject(1);
	}

	@Override
	public boolean removeObject(Compositor entity) {
		AppMainController ctrl = AppMainController.getInstance();
		return ctrl.removeCompositor(entity);
	}

}
